package router

import (
	"gitee.com/lxx_23/lxx-go-fun/utils"
	"github.com/gin-gonic/gin"
	service "module-gin/service/example"
	"net/http"
)

func RegisterTestRouter(r gin.IRouter) {
	// get
	r.GET("/test_get", func(ctx *gin.Context) {
		resp, err := service.TestGet(ctx)
		if err != nil {
			ctx.JSON(
				http.StatusInternalServerError,
				gin.H{"code": 500, "message": "服务器内部错误！", "error": err.Error()},
			)
			return
		}
		ctx.JSON(http.StatusOK, resp)
	})

	// post
	r.POST("/test_post", func(ctx *gin.Context) {
		var req service.TestPostRequest
		if err := ctx.ShouldBind(&req); err != nil {
			ctx.JSON(400, gin.H{"code": 400, "message": utils.GetErrorString("", err)})
			return
		}
		resp, err := service.TestPost(ctx, req)
		if err != nil {
			ctx.JSON(
				http.StatusInternalServerError,
				gin.H{"code": 500, "message": "服务器内部错误！", "error": err.Error()},
			)
			return
		}
		ctx.JSON(http.StatusOK, resp)
	})
}
